﻿/* uncomment this block if there are some conflicts with other js library/framework */
//var $j = jQuery.noConflict();

(function ($) {

/**
* Ajax plugin for AIC App.
*/
AIC.Ajax = {
    /**
    * Ajax load extension with default beforeSend and complete calling <code>App.loading();</code> and some new options.<br />
    * options = {<br />
    *   withLoading: true,<br />
    *   withError: true,<br />
    *   loadingTitle: 'Processing Request',<br />
    *   loadingMessage: 'Processing request. Please wait...'<br />
    *   errorTitle: 'Error',<br />
    *   errorMessage: 'An error occured while processing request.' <br />
    *   preBeforeSend: function (jqXHR, settings) { }, <br />
    *   postBeforeSend: function (jqXHR, settings) { }, <br/>
    *   preError: function (jqXHR, textStatus, errorThrown) { }, <br />
    *   postError: function (jqXHR, textStatus, errorThrown) { }, <br />
    *   preComplete: function (jqXHR, textStatus) { }, <br />
    *   postComplete: function (jqXHR, textStatus) { }, <br />
    *   onAbort: function (jqXHR) { }, <br />
    * };
    */
    load: function (options) {
        var loadingDialog;
        var ajaxInstance;
        var settings = $.extend({
            withLoading: true, // set this option to false to disable loading
            withError: true, // set this option to false to disable alert for error
            loadingTitle: 'Processing Request', // title for loading
            loadingMessage: 'Processing request. Please wait...', // loading message
            errorTitle: 'Error', // error title
            errorMessage: 'An error occured while processing request.', // error message
            preBeforeSend: function (jqXHR, settings) { }, // called in beforeSend, use this instead of overriding beforeSend.
            postBeforeSend: function (jqXHR, settings) { }, // called in beforeSend, use this instead of overriding beforeSend.
            preError: function (jqXHR, textStatus, errorThrown) { }, // called in error, use this instead of overriding error.
            postError: function (jqXHR, textStatus, errorThrown) { }, // called in error, use this instead of overriding error.
            preComplete: function (jqXHR, textStatus) { }, // called in complete, use this instead of overriding complete.
            postComplete: function (jqXHR, textStatus) { }, // called in complete, use this instead of overriding complete.
            onAbort: function (jqXHR) { }, // called after abort. can be replaced by preError or postError with textStatus = "abort"
            beforeSend: function (jqXHR, settings) {
                if (typeof settings.preBeforeSend == 'function')
                    settings.preBeforeSend(jqXHR, settings);
                // need to check whether setting withLoading is true
                if (settings.withLoading === true)
                    loadingDialog = App.loading(settings.loadingMessage, settings.loadingTitle, function () {
                        try {
                            ajaxInstance.abort();
                        } catch (e) { }
                    });
                if (typeof settings.postBeforeSend == 'function')
                    settings.postBeforeSend(jqXHR, settings);
            },
            complete: function (jqXHR, textStatus) {
                if (typeof settings.preComplete == 'function')
                    settings.preComplete(jqXHR, textStatus);
                try {
                    loadingDialog.dismiss();
                } catch (e)
                { }
                if (typeof settings.postComplete == 'function')
                    settings.postComplete(jqXHR, textStatus);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (typeof settings.preError == 'function')
                    settings.preError(jqXHR, textStatus, errorThrown);
                // need to check whether setting withError is true and ajax request is not aborted
                if (settings.withError === true && textStatus != 'abort')
                    App.alert(settings.errorMessage, settings.errorTitle);
                else if (textStatus == 'abort' && (typeof settings.onAbort == 'function'))
                    settings.onAbort(jqXHR);
                if (typeof settings.postError == 'function')
                    settings.postError(jqXHR, textStatus, errorThrown);
            }
        }, options);
        ajaxInstance = $.ajax(settings);
        return ajaxInstance;
    },
	
	//ajaxCall: function (url, method, data, async, fn) {
	ajaxCall: function (url, method, data, fn) {
		//async = typeof (async) != 'undefined' ? async : true;
		this.load({
			url: url,
			type: method,
			dataType: 'json',
			data: data,
			//async: async,
			success: function (data) {
				if (typeof fn == "function") {
					fn(data);
				}
			}
		});
	}

	
};

/* uncomment this block if there are some conflicts with other js library/framework */
})(jQuery);